<script setup lang="ts">
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { useUserStore } from '@/stores/modules/user'
import { ElMessage } from 'element-plus'

const router = useRouter()
const userStore = useUserStore()

const loginForm = ref({
  username: '',
  password: ''
})

const loading = ref(false)

const handleLogin = async () => {
  if (!loginForm.value.username || !loginForm.value.password) {
    ElMessage.warning('请输入用户名和密码')
    return
  }

  loading.value = true
  try {
    const success = userStore.login(loginForm.value.username, loginForm.value.password)
    if (success) {
      ElMessage.success('登录成功')
      router.push('/map')
    } else {
      ElMessage.error('用户名或密码错误')
    }
  } finally {
    loading.value = false
  }
}
</script>

<template>
  <div class="login-container">
    <div class="login-background"></div>
    <el-card class="login-card">
      <template #header>
        <div class="header-content">
          <div class="logo-container">
            <div class="logo-icon"></div>
          </div>
          <h2 class="login-title">土地信息管理系统</h2>
        </div>
      </template>
      <el-form :model="loginForm" label-position="top">
        <el-form-item label="用户名">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
          />
        </el-form-item>
        <el-form-item label="密码">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="success"
            :loading="loading"
            @click="handleLogin"
            class="login-button"
          >
            登录系统
          </el-button>
        </el-form-item>
        <div class="login-tips">
          <p>管理员账号：admin / 123456</p>
          <p>访客账号：visitor / 123456</p>
        </div>
      </el-form>
    </el-card>
  </div>
</template>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-image: url('@/assets/land-background.svg');
  background-size: cover;
  background-position: center;
  filter: blur(0px);
  z-index: -1;
}

.login-card {
  width: 420px;
  border-radius: 8px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(10px);
  background-color: rgba(255, 255, 255, 0.9);
  border: 1px solid rgba(144, 202, 119, 0.5);
  overflow: hidden;
}

.header-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 0;
  background: linear-gradient(to right, #558b2f, #7cb342);
  margin: -20px -20px 10px -20px;
  border-radius: 8px 8px 0 0;
  color: white;
}

.logo-container {
  display: flex;
  justify-content: center;
  margin-bottom: 10px;
}

.logo-icon {
  width: 60px;
  height: 60px;
  background-color: white;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
}

.logo-icon::before {
  content: '';
  position: absolute;
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, #7cb342, #558b2f);
  border-radius: 4px;
  transform: rotate(45deg);
}

.logo-icon::after {
  content: '';
  position: absolute;
  width: 20px;
  height: 20px;
  background-color: white;
  border-radius: 50%;
  border: 2px solid #558b2f;
}

.login-title {
  text-align: center;
  margin: 0;
  color: white;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 1px;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.el-form {
  padding: 10px 20px;
}

.el-form-item {
  margin-bottom: 20px;
}

:deep(.el-input__inner) {
  border-radius: 4px;
  border: 1px solid #dcdfe6;
  transition: all 0.3s;
  padding-left: 12px;
}

:deep(.el-input__inner:focus) {
  border-color: #7cb342;
  box-shadow: 0 0 0 2px rgba(124, 179, 66, 0.2);
}

:deep(.el-form-item__label) {
  color: #558b2f;
  font-weight: 500;
}

.login-button {
  width: 100%;
  height: 44px;
  border-radius: 4px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 1px;
  background: linear-gradient(to right, #558b2f, #7cb342);
  border: none;
  box-shadow: 0 4px 8px rgba(85, 139, 47, 0.3);
  transition: all 0.3s;
}

.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 12px rgba(85, 139, 47, 0.4);
  background: linear-gradient(to right, #4c7e29, #71a439);
}

.login-tips {
  margin-top: 20px;
  text-align: center;
  color: #606266;
  font-size: 14px;
  background-color: rgba(124, 179, 66, 0.1);
  padding: 10px;
  border-radius: 4px;
  border-left: 3px solid #7cb342;
}

.login-tips p {
  margin: 5px 0;
}
</style>